package com.ming.reggie.service.impl;

import com.ming.reggie.entity.User;
import com.ming.reggie.exception.BusinessException;
import com.ming.reggie.mapper.UserMapper;
import com.ming.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 用户登录的方法
     *
     * @param phone       手机号
     * @param userCode    用户输入的验证码
     * @param sessionCode
     * @return
     */
    @Override
    public User login(String phone, String userCode, String sessionCode) {

        if(!userCode.equals(sessionCode)){
            throw new BusinessException("验证码错误");
        }

        User user = userMapper.findByPhone(phone);
        if(user != null) {
            if (user.getStatus() == 1) {
                //用户被禁用
                throw new BusinessException("您的手机号已被禁用");
            }
        }else {
            //如果找不到,完善用户数据,并添加到数据库
            user = new User();
            user.setStatus(0);
            user.setPhone(phone);
            userMapper.addUser(user);

        }

        return user;
    }
}
